package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.GjgxAdapter;
import zj.com.model.Gjgx;
import zj.com.model.JcxDB;
import zj.com.model.Project;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/7/4.
 */
public class GjgxSearchActivity extends  BaseActivity implements View.OnClickListener{


    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv;

    public ListView listView;
    public EditText search_et;

    public Project project;

    public GjgxAdapter adapter;
    public List<Gjgx> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gjgx_search);

        initView();
    }

    public void initView(){
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("关键工序搜索");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("搜索");
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        search_et = (EditText) findViewById(R.id.search_et);

        list = new ArrayList<>();
        adapter = new GjgxAdapter(list,this);
        listView.setAdapter(adapter);

        project = (Project) getIntent().getSerializableExtra("project");

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent=new Intent();
                intent.putExtra("gjgx",list.get(position));
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                search();
                break;
        }
    }

    public ProgressDialog dialog;
    public void search(){

       String keyword =  search_et.getText().toString();
       if(TextUtils.isEmpty(keyword)){

           Toast.makeText(GjgxSearchActivity.this, "请输入你要搜索的关键字", Toast.LENGTH_SHORT).show();
           return;
       }

        if(project == null){
            Toast.makeText(GjgxSearchActivity.this, "请先选择项目", Toast.LENGTH_SHORT).show();
            return ;
        }
        dialog = DialogUtil.createProgressDialog(this,"提示","搜索中");

        NetManager.getInstance(this).GetCriticalActivity(project.getProjectID() + "", keyword, "", 0 + "", 1+"", 15+"", new OnResponseListener<List<Gjgx>>() {
            @Override
            public void onComplete(boolean isOK, List<Gjgx> result, int errorCode, String errorMsg) {

                dialog.dismiss();
                if(isOK){

                    if(result != null && result.size() > 0){
                        list = result;
                        adapter.setDataChange(result);

                    }else{
                        Toast.makeText(GjgxSearchActivity.this, "没有你要搜索的数据", Toast.LENGTH_SHORT).show();
                        list = new ArrayList<Gjgx>();
                        adapter.setDataChange(list);

                    }
                } else{
                    Toast.makeText(GjgxSearchActivity.this, "搜索失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();

                Toast.makeText(GjgxSearchActivity.this, "搜索失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
